#
# telements.py
#
#
# PyEMOF: An EMOF support for Python
#
# Copyright (C) 2004, 2007 Raphael Marvie 
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.
# http://www.fsf.org/licensing/licenses/gpl.html
#
# Author contact: raphael.marvie@lifl.fr
#
# Modified and adapted to conform with ECORE 2.0 by brenomachado@gmail.com


'''
Templates for the generation of EMOF repositories from the definition
of a EMOF compliant model.
'''

__author__  = 'Raphael Marvie <raphael.marvie@lifl.fr> / Breno Batista Machado <brenomachado@gmail.com>'
__date__    = '2008-03-15'
__version__ = '0.7.0'

class tstr:
    pass

class tmpl:
    
    file_header = """# -*- coding: utf-8 -*-
#
# %(name)s.py

from enum import Enum

%(modules_imports)s

class list_composite(list):

    def __init__(self):
        super(list_composite, self).__init__()

    def append(self, value):
        value.isRoot = False
        super(list_composite, self).append(value)

    def remove(self, value):
        value.isRoot = True
        super(list_composite, self).remove(value)
        
"""

    import_template = """import %(module)s"""

    class_header = """

class %(name)s(%(super)s):

    _id = 0
    isRoot = True

    def __init__(self, %(args)s):
        super(%(name)s, self).__init__()
        %(name)s._id += 1
        self._generate_id = 0
        self._xmi_id = '%(name)s:%%s' %% %(name)s._id
"""
    
    enum_decl = """

%(name)s = Enum(%(enum_lit)s)

"""

    simple_attribute_decl = """        self._%(name)s = %(name)s
"""

    multiple_attribute_decl = """        if not %(name)s:
            %(name)s = %(value)s
        self._%(name)s = %(name)s
"""

    test_eq = """
    def __eq__(self, other):
        if hasattr(self, 'visited'):
            return True
        if type(self) != type(other):
            return False
        self.visited = None
        res = %(test)s
        del self.visited
        return res    
"""

    multiple_attribute = """
    def get_%(name)s(self):
        return self._%(name)s
    def add_%(name)s(self, val):
        self._%(name)s.append(val)
    %(name)s = property(get_%(name)s)
"""

    multiple_attribute_enum = """
    def get_%(name)s(self):
        return self._%(name)s
    def add_%(name)s(self, val):
        if val in %(module)s%(enum_name)s:
            self._%(name)s.append(val)
        else:
            raise Exception('Wrong type! Must be a %(enum_name)s')
    %(name)s = property(get_%(name)s)
"""

    multiple_attribute_opposite = """
    def get_%(name)s(self):
        return self._%(name)s
    def add_%(name)s(self, value, other=False):
        self._%(name)s.append(value)
        if other:
            value.%(op)s_%(oname)s(self, False)
    def del_%(name)s(self, value, other=True):
        self._%(name)s.remove(value)
        if other:
            value.del_%(oname)s(self, False)
    %(name)s = property(get_%(name)s)
"""

    readonly_attribute = """
    def get_%(name)s(self):
        return self._%(name)s
    %(name)s = property(get_%(name)s)
"""
    
    readonly_attribute_enum = readonly_attribute

    single_attribute = """
    def get_%(name)s(self):
        return self._%(name)s
    def set_%(name)s(self, value):
        self._%(name)s = value
    %(name)s = property(get_%(name)s, set_%(name)s)
"""

    single_attribute_enum = """
    def get_%(name)s(self):
        return self._%(name)s
    def set_%(name)s(self, value):
        if value in %(module)s%(enum_name)s:
            self._%(name)s = value
        else:
            raise Exception('Wrong type! Must be a %(enum_name)s')
    %(name)s = property(get_%(name)s, set_%(name)s)
"""

    single_attribute_opposite = """
    def get_%(name)s(self):
        return self._%(name)s
    def set_%(name)s(self, value, other=False):
        self._%(name)s = value
        if other:
            value.%(op)s_%(oname)s(self, False)
    def del_%(name)s(self, other=True):
        if other and self._%(name)s:
            self._%(name)s.del_%(oname)s(self, False)
        self._%(name)s = None
    %(name)s = property(get_%(name)s, set_%(name)s)
"""

    operation = """
    def %(name)s(self%(args)s):
        pass
"""

    file_footer = """
# eof
"""
